using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;
using QL_Truong_Hoc.DTO;

namespace QL_Truong_Hoc.DAO
{
	public static class MDonViQuanDoiDAO
	{
		#region Methods

		/// <summary>
		/// Saves a record to the mDonViQuanDoi table.
		/// </summary>
		public static bool Insert(Database objDb, DbTransaction objTrans, MDonViQuanDoiDTO mDonViQuanDoi)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mDonViQuanDoi", mDonViQuanDoi);

			object[] parameters = new object[]
			{
				Null.GetNull(mDonViQuanDoi.Dv_ten, ""),
				Null.GetNull(mDonViQuanDoi.Dv_diachi, ""),
				Null.GetNull(mDonViQuanDoi.Dv_thutu, DBNull.Value),
				Null.GetNull(mDonViQuanDoi.Dv_ma, ""),
				Null.GetNull(mDonViQuanDoi.Dv_cha, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mDonViQuanDoiInsert", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mDonViQuanDoiInsert", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mDonViQuanDoiInsert", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Updates a record in the mDonViQuanDoi table.
		/// </summary>
		public static bool Update(Database objDb, DbTransaction objTrans, MDonViQuanDoiDTO mDonViQuanDoi)
		{
bool blnResult = false;
try
{
			clsValidationUtility.ValidateArgument("mDonViQuanDoi", mDonViQuanDoi);

			object[] parameters = new object[]
			{
				Null.GetNull(mDonViQuanDoi.DV_ID, DBNull.Value),
				Null.GetNull(mDonViQuanDoi.Dv_ten, ""),
				Null.GetNull(mDonViQuanDoi.Dv_diachi, ""),
				Null.GetNull(mDonViQuanDoi.Dv_thutu, DBNull.Value),
				Null.GetNull(mDonViQuanDoi.Dv_ma, ""),
				Null.GetNull(mDonViQuanDoi.Dv_cha, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mDonViQuanDoiUpdate", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mDonViQuanDoiUpdate", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mDonViQuanDoiUpdate", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mDonViQuanDoi table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, int dV_ID)
		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{
Null.GetNull(dV_ID, DBNull.Value)
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mDonViQuanDoiDelete", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mDonViQuanDoiDelete", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mDonViQuanDoiDelete", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Deletes a record from the mDonViQuanDoi table by its primary key.
		/// </summary>
		public static bool Delete(Database objDb, DbTransaction objTrans, string strWhere)		{
bool blnResult = false;
try
{
			object[] parameters = new object[]
			{strWhere
			};

int intResult;
if (objTrans == null)
{ intResult = objDb.ExecuteNonQuery("mDonViQuanDoiDeleteWhere", parameters); }
else if (objTrans.Connection == null)
{ intResult = objDb.ExecuteNonQuery("mDonViQuanDoiDeleteWhere", parameters); }
else
{ intResult = objDb.ExecuteNonQuery(objTrans, "mDonViQuanDoiDeleteWhere", parameters); }
if (intResult > 0) return true;
}
catch { return false; }
return blnResult;
		}

		/// <summary>
		/// Selects a single record from the mDonViQuanDoi table.
		/// </summary>
		public static MDonViQuanDoiDTO Select(Database objDb, int dV_ID)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(dV_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mDonViQuanDoiSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects a single record from the mDonViQuanDoi table.
		/// </summary>
		public static DataTable Select(Database objDb, int dV_ID, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{
Null.GetNull(dV_ID, DBNull.Value)
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mDonViQuanDoiSelect", parameters))
			{
				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Get next index from the mDonViQuanDoi table.
		/// </summary>
		public static int SelectNextIndex(Database objDb)		{
int intResult = -1;
try{
IDataReader dr = objDb.ExecuteReader("mDonViQuanDoiSelectNextIndex");
if (dr != null)
{
DataTable dt = Untils.ConvertDataReaderToDataTable(dr, true);
intResult = clsCommon.fncCnvNullToInt(dt.Rows[0][0]);
}
}catch { }
return intResult;
		}

		/// <summary>
		/// Selects all records from the mDonViQuanDoi table.
		/// </summary>
		public static List<MDonViQuanDoiDTO> SelectAll(Database objDb)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mDonViQuanDoiSelectAll"))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mDonViQuanDoi table.
		/// </summary>
		public static List<MDonViQuanDoiDTO> SelectWhere(Database objDb, string strWhere)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mDonViQuanDoiSelectWhere", parameters))
			{

				return MapDataReaderList(dataReader);
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mDonViQuanDoi table.
		/// </summary>
		public static DataTable SelectAll(Database objDb, bool blnReturnDatatable = true)
		{
try{
			using (IDataReader dataReader = objDb.ExecuteReader("mDonViQuanDoiSelectAll"))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Selects all records from the mDonViQuanDoi table.
		/// </summary>
		public static DataTable SelectWhere(Database objDb, string strWhere, bool blnReturnDatatable = true)
		{
try{
			object[] parameters = new object[]
			{strWhere
			};

			using (IDataReader dataReader = objDb.ExecuteReader("mDonViQuanDoiSelectWhere", parameters))
			{

				if(dataReader.FieldCount > 0)
				{
				DataTable dt = new DataTable();
				dt.Load(dataReader);
					return dt;
				}
				else
				{
					return null;
				}
			}
}catch { return null; }
		}

		/// <summary>
		/// Creates a new instance of the MDonViQuanDoiDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static MDonViQuanDoiDTO MapDataReader(IDataReader dataReader)
		{
			MDonViQuanDoiDTO mDonViQuanDoiDTO = new MDonViQuanDoiDTO();
DataTable dt = new DataTable();
dt.Load(dataReader);
if (dt.Rows.Count <= 0) return mDonViQuanDoiDTO;
			mDonViQuanDoiDTO.DV_ID = clsCommon.fncCnvNullToInt(dt.Rows[0]["DV_ID"]);
			mDonViQuanDoiDTO.Dv_ten = clsCommon.fncCnvNullToString(dt.Rows[0]["dv_ten"]);
			mDonViQuanDoiDTO.Dv_diachi = clsCommon.fncCnvNullToString(dt.Rows[0]["dv_diachi"]);
			mDonViQuanDoiDTO.Dv_thutu = clsCommon.fncCnvNullToInt(dt.Rows[0]["dv_thutu"]);
			mDonViQuanDoiDTO.Dv_ma = clsCommon.fncCnvNullToString(dt.Rows[0]["dv_ma"]);
			mDonViQuanDoiDTO.Dv_cha = clsCommon.fncCnvNullToInt(dt.Rows[0]["dv_cha"]);

			return mDonViQuanDoiDTO;
		}
		/// <summary>
		/// Creates a new instance of the MDonViQuanDoiDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private static List<MDonViQuanDoiDTO> MapDataReaderList(IDataReader dataReader)
		{
				List<MDonViQuanDoiDTO> lstResult = new List<MDonViQuanDoiDTO>();
			MDonViQuanDoiDTO mDonViQuanDoiDTO;
DataTable dt = new DataTable();
dt.Load(dataReader);
for (int i = 0; i <= dt.Rows.Count-1; i++)
{
			mDonViQuanDoiDTO = new MDonViQuanDoiDTO();
			mDonViQuanDoiDTO.DV_ID = clsCommon.fncCnvNullToInt(dt.Rows[i]["DV_ID"]);
			mDonViQuanDoiDTO.Dv_ten = clsCommon.fncCnvNullToString(dt.Rows[i]["dv_ten"]);
			mDonViQuanDoiDTO.Dv_diachi = clsCommon.fncCnvNullToString(dt.Rows[i]["dv_diachi"]);
			mDonViQuanDoiDTO.Dv_thutu = clsCommon.fncCnvNullToInt(dt.Rows[i]["dv_thutu"]);
			mDonViQuanDoiDTO.Dv_ma = clsCommon.fncCnvNullToString(dt.Rows[i]["dv_ma"]);
			mDonViQuanDoiDTO.Dv_cha = clsCommon.fncCnvNullToInt(dt.Rows[i]["dv_cha"]);
lstResult.Add(mDonViQuanDoiDTO);
}

			return lstResult;
		}

		#endregion
	}
}
